﻿// version 0.2

var Htmls = (function () {
	var attrs = 
		['id','class','style','title','lang','dir','accesskey','tabindex','charset','type','name','href','hreflang','rel','rev','shape','coords','src','alt','longdesc','height','width','usemap','ismap','action','method','enctype','accept','accept-charset','for','value','checked','disabled','readonly','size','maxlength','summary','border','frame','rules','cellspacing','cellpadding','align','char','charoff','valign','abbr','axis','headers','scope','rowspan','colspan'];


	var tags = 
		['html','head','title','base','meta','link','style','script','noscript','body','div','p','h1','h2','h3','h4','h5','h6','ul','ol','li','dl','dt','dd','address','hr','pre','blockquote','ins','del','a','span','bdo','br','em','strong','dfn','code','samp','kbd','var','cite','abbr','acronym','q','sub','sup','tt','i','b','big','small','object','param','img','map','area','form','label','input','select','optgroup','option','textarea','fieldset','legend','button','table','caption','thead','tfoot','tbody','colgroup','col','tr','th','td'];
	var selfCloseTags = {'base':'','meta':'','link':'','hr':'','br':'','img':'','area':'','input':''};

	function HtmlTag(name, isSelfClose){
		this.name = name;
		this.isSelfClose = isSelfClose;
		this.attrs = {};
		this.children = [];
	}

	for(var a=0;a<attrs.length;a++){
		var attr = attrs[a];
		var methodName = attr[0].toUpperCase() + attr.substr(1, attr.length - 1);
		HtmlTag.prototype[methodName] = (function(p){
			return function(x){ return this.Attr(p,x); };
		})(attr);
	}

	HtmlTag.prototype.Attr = function(name, value){
		if(name.constructor == Object){
			for (var j in name) {
				if (name.hasOwnProperty(j)) {
					this.attrs[j] = name[j];
				}
			}
		}else{
			this.attrs[name] = value;
		}
		return this;
	};

	HtmlTag.prototype.$ = function(){
		for (var i = 0; i < arguments.length; i++) {
			var arg = arguments[i];
			if(arg == undefined)
				continue;
			if (arg.constructor == Array) {
				for (var j = 0; j < arg.length; j++) {
					this.children.push(arg[j]);
				}
			}else if (arg.constructor == Object) {
				this.Attr(arg);
			}else{
				this.children.push(arg);
			}
		}
		return this;
	};

	HtmlTag.prototype.render = function(buffer){
		buffer.push("<" + this.name);
		for (var attr in this.attrs) {
			if (this.attrs.hasOwnProperty(attr)) {
				buffer.push(" " + attr + "=\"" + this.attrs[attr] + "\"");
			}
		}
		if(this.isSelfClose){
			buffer.push("/>");
		}
		else{
			buffer.push(">");
			for (var j = 0; j < this.children.length; j++) {
				var child = this.children[j];
				if(child instanceof HtmlTag){
					child.render(buffer);
				}else{
					buffer.push(child.toString());
				}
			}
			buffer.push("</" + this.name + ">");
		}
		return buffer;
	};

	HtmlTag.prototype.toString = function(){
		return this.render([]).join("");
	};

	var result = {};
	for(var t=0;t<tags.length;t++){
		var tagName = tags[t];
		result[tagName] = 
			(function(p){
			return function () { var tag = new HtmlTag(p, p in selfCloseTags); return tag.$.apply(tag, arguments);};
		})(tagName);
	}
	return result;
})();


